package com.miyake.dsp.converter;

import com.miyake.dsp.ComplexArray;
import com.miyake.dsp.Processor;

public class Log implements Processor {

	private ComplexArray source;
	private LogSetting setting;

	public Log(LogSetting setting) {
		this.setting = setting;
	}

	@Override
	public void setSource(ComplexArray source) {
		this.source = source;
	}

	@Override
	public ComplexArray getProcessed() {	
		boolean bImg = false;
		double[] real = source.getRealPart().clone();
		double[] img = null;
		if (source.getImagPart() != null) {
			bImg = true;
			img = source.getImagPart().clone();
		}
		for (int i = 0; i < source.getRealPart().length; i++) {
			real[i] = getLog(source.getRealPart()[i]);
			if (bImg) {
				img[i] = getLog(source.getImagPart()[i]);
			}
		}
		
		return new ComplexArray(real, img);
	}
	
	private double getLog(double v) {
		if (v > 0) {
			double v2 = setting.getMagnitude() * Math.log10(v);
			if (v2 > setting.getBottom()) {
				return v2;
			}
		}

		return setting.getBottom() ;
	}

	@Override
	public ComplexArray getSource() {
		return this.source;
	}

	@Override
	public Object getSetting() {
		return setting;
	}
}
